package front

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	frontDao "hft/Http/Models/dao/front"
	"hft/Http/Models/request/front"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"strconv"
	"strings"
)

type Active struct{}

func (*Active) GetActiveList(paginate *front.ActivePage, sid int) (list *utils.Page, err error) {

	var region *admin.Region

	err = global.OC_DB.First(&region, sid).Error

	if err != nil {
		return nil, errors.New("该城市不存在")
	}

	var acs = make([]*front.ActiveList, 0)

	var acsBase []*admin.ActiveBase

	var strs = strings.Split(paginate.ActiveGroup, ",")
	var ids = make([]int, 0)
	for _, v := range strs {
		var id, _ = strconv.Atoi(v)
		if id != 0 {
			ids = append(ids, id)
		}
	}

	if paginate.PageSize == 0 {
		global.OC_DB.Model(&acsBase).Scopes(frontDao.ActiveDao.List(paginate, ids, fmt.Sprintf("%v%v,%%", region.Link, region.ID))).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)

		paginate.Data = acs
		return &paginate.Page, nil
	}

	global.OC_DB.Scopes(frontDao.ActiveDao.List(paginate, ids, fmt.Sprintf("%v%v,%%", region.Link, region.ID))).Find(&acsBase).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)

	for _, v := range acsBase {

		var cont, _ = utils.ConvertHTMLToPlainText(v.Cont.Content)
		if len(cont) >= 800 {
			cont = cont[:800]
		}

		acs = append(acs, &front.ActiveList{
			Name:        v.Name,
			Cover:       v.Cover,
			HasImg:      v.HasImg,
			HasVideo:    v.HasVideo,
			CreateTime:  v.CreateTime,
			ActiveGroup: v.ActiveGroup,
			Cont:        cont,
			ID:          v.ID,
			LouPanId:    v.LouPanId, //楼盘id
		})
	}

	paginate.Data = acs

	return &paginate.Page, nil

}

func (*Active) GetActiveInfo(id int) (data *front.ActiveInfo, err error) {

	var activeCont *admin.ActiveBase
	err = global.OC_DB.Preload("Cont").First(&activeCont, id).Error
	fmt.Println(activeCont.LouPanId)
	if err != nil {
		return nil, errors.New(e.ActiveNotExist)
	}
	var ids *front.IDS
	var activeID *admin.ActiveBase
	global.OC_DB.Raw(fmt.Sprintf("SELECT  (SELECT id FROM hft_active_base WHERE id > %v AND lou_pan_id = %v ORDER BY id LIMIT 1) AS next_id, (SELECT id FROM hft_active_base WHERE id < %v AND lou_pan_id = %v ORDER BY id DESC LIMIT 1) AS prev_id FROM hft_active_base WHERE id = %v;", id, activeCont.LouPanId, id, activeCont.LouPanId, id)).Scan(&ids)
	if ids.PrevID == 0 {

		global.OC_DB.Model(&admin.ActiveBase{}).Where("lou_pan_id = ?", activeCont.LouPanId).Select("id").Order("id DESC").Limit(1).Scan(&activeID)
		ids.PrevID = activeID.ID
	}
	if ids.NextID == 0 {
		global.OC_DB.Model(&admin.ActiveBase{}).Where("lou_pan_id = ?", activeCont.LouPanId).Select("id").Order("id ASC").Limit(1).Scan(&activeID)
		ids.NextID = activeID.ID
	}
	var active = &front.ActiveInfo{
		Title:       activeCont.Name,
		Cont:        activeCont.Cont.Content,
		ID:          activeCont.ID,
		PreID:       ids.PrevID,
		NextID:      ids.NextID,
		ActiveGroup: activeCont.ActiveGroup,
		CreateTime:  int(activeCont.CreateTime.UnixMilli()),
	}

	return active, nil

}

var ActiveService = new(Active)
